package com.yhl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhl.entity.Bill;
import com.yhl.entity.Provider;
import com.yhl.mapper.BillMapper;
import com.yhl.mapper.ProviderMapper;
import com.yhl.service.BillService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @author YaoHongli
* @since 2023-03-20 14:47:43
*/
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;

    @Autowired
    private ProviderMapper providerMapper;
    @Override
    public void addBill(Bill bill){
        billMapper.insert(bill);
    }
    @Override
    public void deleteBill(String id){
        billMapper.deleteById(id);
    }
    @Override
    public void updateBill(Bill bill){
        billMapper.updateById(bill);
    }
    @Override
    public Bill getBillById(String id){
        return billMapper.selectById(id);
    }
    @Override
    public IPage<Bill> getBillByPage(Page page, Bill bill){
        LambdaQueryWrapper<Bill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(bill.getProductName())){
            lambdaQueryWrapper.like(Bill::getProductName,bill.getProductName());
        }
        if (bill.getProviderId()!=0){
            lambdaQueryWrapper.eq(Bill::getProviderId,bill.getProviderId());
        }
        if(bill.getIsPayment()!=0){
            lambdaQueryWrapper.eq(Bill::getIsPayment,bill.getIsPayment());
        }
        IPage<Bill> billIPage = billMapper.selectPage(page,lambdaQueryWrapper);

        for (Bill bill1:billIPage.getRecords()) {
            Provider provider = providerMapper.selectById(bill1.getProviderId());
            bill1.setProviderName(provider.getProName());
        }
        return billIPage;
    }
}

